package com.lfs.adapter.init;

import com.pdd.pop.sdk.http.HttpClientConfig;
import com.pdd.pop.sdk.http.PopHttpClient;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
@Slf4j
public class PopClientInit {

    @Value("${pdd.clientId:123}")
    private String clientId;

    @Value("${pdd.clientSecret:123}")
    private String clientSecret;

    @Value("${pdd.connectionTimeOut:2000}")
    private int connectionTimeOut;

    @Value("${pdd.socketTimeOut:2000}")
    private int socketTimeOut;

    @Value("${pdd.connectionRequestTimeOut:1000}")
    private int connectionRequestTimeOut;

    private PopHttpClient popClient;

    @PostConstruct
    public void init() {
        try {
            HttpClientConfig config = HttpClientConfig.getDefault();
            config.setConnectionTimeoutMillis(connectionTimeOut);
            config.setSocketTimeoutMillis(socketTimeOut);
            config.setConnectionRequestTimeout(connectionRequestTimeOut);
            popClient = new PopHttpClient(clientId, clientSecret, config);
            log.info("PopHttpClient initialized successfully. HashCode: {}", popClient.hashCode());
        } catch (Exception e) {
            log.error("Failed to initialize PopHttpClient", e);
        }
    }

    public PopHttpClient getPopClient() {
        log.info("Returning PopHttpClient instance. HashCode: {}", popClient.hashCode());
        return popClient;
    }
}